﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PegasCompany.Data;
using PegasCompany.Tools;
using Resources;

namespace PegasCompany
{
    public partial class Services : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Title = String.Format("{0} - {1}", UserSite.resCompanyName, UserSite.resServices);

            if (!Page.IsPostBack)
            {
                if (!Request.QueryString.HasKeys())
                {
                    FillMessages();
                }
                else if (!String.IsNullOrEmpty(Request.QueryString["Id"]))
                {
                    string sSiteContentId = Request.QueryString["Id"];
                    int iSiteContentId;

                    if (int.TryParse(sSiteContentId, out iSiteContentId))
                    {
                        SiteContent sc = SiteContent.Get(iSiteContentId);
                        if (sc != null)
                        {
                            mvCompanyMessages.ActiveViewIndex = 1;
                            lContentTitle.Text = sc.Title;
                            lContentDescription.Text = sc.Description;
                            
                            lTitle.Text = sc.Title;
                            lDate.Text = sc.CreateDate.ToString();
                            lContent.Text = sc.Content;
                            
                            //rptFiles.DataSource = sc.FileAttachements;
                            //rptFiles.DataBind();

                            Title = String.Format("{0} - {1}", UserSite.resCompanyName, sc.Title);
                        }
                    }
                }
            }
        }

        private void FillMessages()
        {
            mvCompanyMessages.ActiveViewIndex = 0;

            lContentTitle.Text = UserSite.resServices;

            List<SiteContent> scList = SiteContent.ListAllContent((int)EnumHelper.ListContentType.Services);

            List<int> DistinctYear = scList.Select(p => new { p.CreateDate.Value.Year }).Distinct().ToList() // excutes query
                .Select(x => x.Year).ToList();

            foreach (int i in DistinctYear)
            {
                List<int> DistinctMonth = scList.Select(p => new { i, p.CreateDate.Value.Month }).Distinct().ToList() // excutes query
                .Select(x => x.Month).ToList();
            }

            //List<DateTime> DistinctYearMonths = DistinctYear.Select(p => new { p.CreateDate.Value.Year, p.CreateDate.Value.Month }).Distinct().ToList() // excutes query
            //    .Select(x => new DateTime(x.Year, x.Month, 1)).ToList();

            rptList.DataSource = scList.OrderByDescending(t => t.CreateDate);
            rptList.DataBind();
        }
    }
}